package cn.getech.data.development.model.vo;

import cn.getech.data.development.constant.RealTableType;
import cn.getech.data.development.constant.YesOrNoEnum;
import cn.getech.data.development.entity.flink.BdpRealTableField;
import cn.getech.data.development.entity.flink.BdpRealTableInfo;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Data
public class BdpRealTableInfoVO {

    /**
     * 编号
     */
    private Integer id;

    /**
     * 资源id
     */
    private Integer resourceId;

    /**
     * 表名称
     */
    private String tableName;
    /**
     * 表分层
     */
    private String dbName;
    /**
     * topic
     */
    private String topicName;


    /**
     * 内部部表 1   外部表 2
     */
    private Integer tableType;

    /**
     * 类目id
     */
    private Set<Integer> categoryIds;
    /**
     * 主题id
     */
    private Set<Integer> subjectIds;

    /**
     * 是否分区  1是 0 否
     */
    private Integer partitionFlag;
    /**
     * 分区数
     */
    private Integer partitionNum;
    /**
     * 副本数
     */
    private Short duplicateNum;



    /**
     * 分隔符
     */
    private Integer delimitId;

    /**
     * 格式
     */
    private String format;
    /**
     * 描述信息
     */
    private String tableDesc;

    /**
     * 其他参数
     */
    private JSONObject config;

    /**
     * 访问记录
     */
    private Integer viewNum;

    /**
     * 和修改创建表时候 给固定角色授权
     */
    private Long roleId;

    private   Long createUser ;
    /**
     * 字段属性
     */
    private List<BdpRealTableField> fieldList= Lists.newArrayList();

    public String verificationParam() {
        if (tableType == null) {
            return "表类型不能为空";
        }
        if (resourceId == null && RealTableType.OUTER.getCode().equals(tableType)) {
            return "资源不能为空";
        }
        if (StringUtils.isEmpty(tableName)) {
            return "表名不能为空";
        }
        if (!tableName.matches("[A-Za-z0-9_]+")) {
            return "表名只能包含字母,数字,下划线";
        }
        if (tableName.length() > 100) {
            return "表名长度不能超过100";
        }
        if (StringUtils.isEmpty(topicName)) {
            return "topic不能为空";
        }
        if (!topicName.matches("[A-Za-z0-9_\\-]+")) {
            return "topic只能包含字母,数字,下划线,中划线";
        }
        if (topicName.length() > 100) {
            return "topic长度不能超过100";
        }
        if (StringUtils.isEmpty(format)) {
            return "格式不能为空";
        }
        if (CollectionUtils.isEmpty(fieldList)) {
            return "字段信息不能为空";
        }
        Set<String> names = fieldList.stream().map(BdpRealTableField::getFieldName).collect(Collectors.toSet());
        if (names.size() != fieldList.size()) {
            return "字段名不能重复";
        }
        if (YesOrNoEnum.YES.getValue().equals(partitionFlag)) {
            if (partitionNum == null) {
                return "分区数不能为空";
            }
            if (duplicateNum == null) {
                return "副本数不能为空";
            }
        }
        for (BdpRealTableField field : fieldList) {
            if (field.getFieldName().length() > 100) {
                return "字段名长度不能超过100";
            }
            if (StringUtils.isNotEmpty(field.getFieldDesc())) {
                if (field.getFieldDesc().length() > 100) {
                    return "字段描述长度不能超过100";
                }
            }
            if (!field.getFieldName().matches("[A-Za-z0-9_]+")) {
                return "字段名只能包含字母,数字,下划线";
            }
            if (StringUtils.isNotEmpty(field.getFieldDesc())) {
                if (field.getFieldDesc().length() > 200) {
                    return "字段描述长度不能超过200";
                }
            }
        }
        return null;
    }

    public BdpRealTableInfo toDto() {
        BdpRealTableInfo tableInfo = new BdpRealTableInfo(
                id, resourceId, tableName, topicName, delimitId,
                format, tableDesc, config.toJSONString());
        tableInfo.setDbName(dbName);
        tableInfo.setTableType(tableType);
        tableInfo.setPartitionFlag(partitionFlag);
        tableInfo.setDuplicateNum(duplicateNum);
        tableInfo.setPartitionNum(partitionNum);
        return tableInfo;
    }
    public BdpRealTableInfoVO(BdpRealTableInfo table){
        this.id = table.getId();
        this.resourceId = table.getResourceId();
        this.tableType=table.getTableType();
        this.tableName = table.getTableName();
        this.topicName = table.getTopicName();
        this.delimitId = table.getDelimitId();
        this.dbName = table.getDbName();
        this.format = table.getFormat();
        this.createUser = table.getCreateUser();
        this.tableDesc = table.getTableDesc();
        this.tableType = table.getTableType();
        this.partitionFlag = table.getPartitionFlag();
        this.partitionNum = table.getPartitionNum();
        this.duplicateNum = table.getDuplicateNum();
        try {
            this.config = JSONObject.parseObject(table.getConfig());
        } catch (Exception ignored) {
        }
        this.viewNum = table.getViewNum();
    }



    public BdpRealTableInfoVO() {

    }

    //实时表关联的目录id
    private List<String> cateGoryIdsList;

}
